//
//  DeviceTypeImageViewController.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/27.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

class DeviceTypeImageViewController: BaseViewController, BottomShadow, UICollectionViewDataSource, UICollectionViewDelegate {
    let itemCount: CGFloat = 4
    let bottomAndTopMargin: CGFloat = 24
    let imageViewNames = ["btn_cycle", "btn_home", "btn_car", "btn_company"]
    @IBOutlet weak var mainView: UIView!
    @IBOutlet weak var selectImageCollection: UICollectionView!
    var selectImage:((UIImage) -> Void)?
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        // Do any additional setup after loading the view.
    }
    
    func setUI() {
        titleLabel(text: "创建场景", nil)
        setShadow(view: mainView, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: CORNERRADIUS)
        selectImageCollection.layer.cornerRadius = CORNERRADIUS
        selectImageCollection.layer.masksToBounds = true
        selectImageCollection.register(UINib(nibName: "\(DeviceTypeCollectionViewCell.self)", bundle: nil), forCellWithReuseIdentifier: "\(DeviceTypeCollectionViewCell.self)")
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .vertical
        let size = UIImage(named: "\(imageViewNames[0])")?.size
        guard let itemSize = size else { return assert(false, "图片不存在") }
        flowLayout.itemSize = CGSize(width: itemSize.width, height: itemSize.height)
        let itemLRMargin = (mainView.width - itemSize.width * itemCount) / (itemCount - 1)
        
        let width = (mainView.width - itemLRMargin * 2 - itemSize.width * itemCount)
        let itemMargin = width / (itemCount - 1)
        
        flowLayout.minimumInteritemSpacing = itemMargin
        flowLayout.sectionInset = UIEdgeInsetsMake(bottomAndTopMargin, itemLRMargin, bottomAndTopMargin, itemLRMargin)
        flowLayout.minimumLineSpacing = bottomAndTopMargin
        selectImageCollection.collectionViewLayout = flowLayout
        selectImageCollection.reloadData()
    }
    
    override func setNavigation() {
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageViewNames.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "\(DeviceTypeCollectionViewCell.self)", for: indexPath) as? DeviceTypeCollectionViewCell
        cell?.showImageView.image = UIImage(named: imageViewNames[indexPath.row])
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectImage?(UIImage(named: imageViewNames[indexPath.row]) ?? UIImage())
        navigationController?.popViewController(animated: true)
    }
    
}
